home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM 1996 Fall / BMUG Fall'96 PD-ROM.iso / Education / Math / MathPad 2.4 / Examples / incl / histogram < prev    next >
Text File  |  1996-03-25  |  768b  |  28 lines

  1. -- histo(array,lo,hi,nbins) accumulates a histogram of values from 'array'. Returns the histogram in 'bins'. Also calculates total,mean and stdev of values between 'lo' and 'hi'. See also: "histogram" XFun.
  2.  
  3. -- max(array) finds max value in array
  4.  
  5. histo(array,lo,hi,nbins) = total := 0,
  6.    sumn := 0, sumsq := 0,
  7.    scl := nbins/(hi-lo),
  8.    bins:=0[:nbins],
  9.    i:=1,
  10.    (
  11.     num:=array[i],
  12.     i:=i+1,
  13.     (j := trunc((num-lo)*scl)+1,
  14.      bins[j] := bins[j]+1,
  15.      total := total+1,
  16.      sumn := sumn+num,
  17.      sumsq := sumsq + num*num) when num≥lo and num<hi) while i ≤ count(array)
  18.  
  19. mean = sumn/total
  20. stdev = sqrt((sumsq-sumn*sumn/total)/(total-1))
  21.  
  22. max(array) = maxv:=-1/0,
  23.   i:=0,
  24.   (i:=i+1,
  25.    maxv:=array[i] when array[i]>maxv) while i≤ count(array),
  26.   maxv
  27.  
  28.